package ygg.saw
{
	import flash.desktop.NativeApplication;
	import flash.display.*;
	import flash.system.*;
	import flash.events.*;
	import flash.utils.getTimer;
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;

	import ygg.filetype.swf.*;
	import ygg.exporter.swf.*;
	import ygg.encoding.swf.tag.*;
	
	import j.sus2maj7.exporter.ExporterContext;
	
	public class YggSaw extends Sprite
	{
		private var _swf:Swf;
		
		public function YggSaw()
		{
			super();
			
			init();
			
			//loadSwfFile(new File("C:\\Users\\sad\\Desktop\\so\\so.swf") );
		}
		
		private function init():void
		{
			
			//trace( 'trying file');
			var swfFilter:FileFilter = new FileFilter("swf", "*.swf");
			var f:File = new File();
			try
			{
				f.browseForOpen("open swf", [swfFilter]);
				f.addEventListener(Event.SELECT, onSelectFile);
			}
			catch (error:Error)
			{
				trace("Failed:", error.message);
			}
		}
		
		private function onSelectFile(e:Event):void
		{
			trace ("onSelectFile");
			loadSwfFile(File(e.target));

		}
		
		private function loadSwfFile(file:File):void
		{
			if (file == null || !file.exists)
			{
				trace ("doesn't exist");
				return;
			}
			
			try
			{
				trace( 'read');
				var bytes:ByteArray = readSWFFile(file);
				//bytes.endian = Endian.LITTLE_ENDIAN;
				trace( 'parse\n');
				
				_swf = new Swf();
				_swf.setBytes(bytes);
				
				_swf.decode(); //splits tags up but doesnt decode them
				
				for each (var te:TagEncoding in _swf.tags)
				{
					trace('tag code', te.type);
					te.decode();
				}
				
				trace( _swf.header.toString());
				/*
				var swfExporter:SwfExporter = new SwfExporter();
				var context:ExporterContext = new ExporterContext();
				context.vars.maxLevels = 1;
				var rootDir:File = new File("C:\\Users\\sad\\Desktop\\export");
				context.vars.rootDir = new File("C:\\Users\\sad\\Desktop\\export");
				context.vars.imageDir = rootDir.resolvePath('image/');
				context.vars.binaryDir = rootDir.resolvePath('bin/');
				context.vars.classDir = rootDir.resolvePath('class/');
				swfExporter.export(_swf, context);
				
				//NativeApplication.nativeApplication.exit(0);
				//*/
				exportToFolder();
			}
			catch (error:Error)
			{
				trace( 'error');
				
				trace (error);
				trace (error.getStackTrace());
				
				return;
			}

		}

		private function readSWFFile(file:File):ByteArray
		{
			trace( 'readSWFFile', file.nativePath);
			var bytes:ByteArray = new ByteArray();
			var fileStream:FileStream = new FileStream();
			fileStream.open(file,FileMode.READ);
			fileStream.readBytes(bytes, 0, 0);
			
			fileStream.close();
			trace ("bl:" + bytes.length);
			return bytes;
		}
		
		
		private function exportToFolder():void
		{
			//ask for destination
			var f:File = new File();
			try
			{
				f.addEventListener(Event.SELECT, onSelectExportDir);
				f.browseForDirectory("Select Destination");
				
			}
			catch (error:Error)
			{
				trace("Failed:", error.message);
			}
			
		}
		
		private function onSelectExportDir(e:Event):void
		{
			var rootDir:File = File(e.target)
			
			if (rootDir.exists && rootDir.isDirectory)
			{
				var swfExporter:SwfExporter = new SwfExporter();
				var context:ExporterContext = new ExporterContext();
				context.vars.maxLevels = 1;
				
				context.vars.rootDir = rootDir;
				context.vars.imageDir = rootDir.resolvePath('image/');
				context.vars.binaryDir = rootDir.resolvePath('bin/');
				context.vars.classDir = rootDir.resolvePath('class/');
				swfExporter.export(_swf, context);
				
				NativeApplication.nativeApplication.exit(0);
			}
			
		}
		
		
	}
}